package reflect;

import java.lang.reflect.Constructor;
import java.lang.reflect.InvocationTargetException;

/**
 * @author DBX
 * @effect
 * @date 2021/09/15/10:37
 */
public class ReflectDemo2 {
    /*
    * 使用反射实例化对象
    * 1.使用类对象的newInstance方法实例化，无参构造器
    * 2.通过类对象获取构造器。然后通过构造器对象实例化对象*/

    public static void main(String[] args) throws ClassNotFoundException, NoSuchMethodException, IllegalAccessException, InvocationTargetException, InstantiationException {
    User user = new User();
        System.out.println(user);

        Class cls = Class.forName("reflect.User");
        Constructor c = cls.getConstructor(String.class,String.class,int.class);
        Object o = c.newInstance("ls","男",21);
        System.out.println(o);
    }

}
